package com.senseuni.da.http;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

public class OkHttpManager {

    public static final long DEFAULT_TIMEOUT = 30 * 1000;

    public static OkHttpClient getHttpClient() {
        return OkHttpClientHolder.Instance;
    }

    private static class OkHttpClientHolder {
        private static final OkHttpClient Instance = createClient();

        private static OkHttpClient createClient() {
            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            OkHttpClient.Builder builder = new OkHttpClient.Builder();

            builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS).
                    writeTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS).
                    readTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);

            builder.addInterceptor(httpLoggingInterceptor);
            return builder.build();
        }
    }

}
